import { cloneDeep, } from 'lodash-es';

let deptList: DeptRecord[] = [
  {createBy: "admin", createTime: "2024-07-07 17:41:52", updateBy: null, updateTime: null, remark: null, deptId: 100, parentId: 0, ancestors: "0", deptName: "必哥科技", orderNum: 0, leader: "必哥", phone: "15888888888", email: "bg@qq.com", status: "0", delFlag: "0", parentName: null, children: []},
  {createBy: "admin", createTime: "2024-07-07 17:41:52", updateBy: null, updateTime: null, remark: null, deptId: 101, parentId: 100, ancestors: "0,100", deptName: "深圳总公司", orderNum: 1, leader: "必哥", phone: "15888888888", email: "bg@qq.com", status: "0", delFlag: "0", parentName: null, children: []},
  {createBy: "admin", createTime: "2024-07-07 17:41:52", updateBy: null, updateTime: null, remark: null, deptId: 102, parentId: 100, ancestors: "0,100", deptName: "长沙分公司", orderNum: 2, leader: "必哥", phone: "15888888888", email: "bg@qq.com", status: "0", delFlag: "0", parentName: null, children: []},
  {createBy: "admin", createTime: "2024-07-07 17:41:52", updateBy: null, updateTime: null, remark: null, deptId: 103, parentId: 101, ancestors: "0,100,101", deptName: "研发部门", orderNum: 1, leader: "必哥", phone: "15888888888", email: "bg@qq.com", status: "0", delFlag: "0", parentName: null, children: []},
  {createBy: "admin", createTime: "2024-07-07 17:41:52", updateBy: null, updateTime: null, remark: null, deptId: 104, parentId: 101, ancestors: "0,100,101", deptName: "市场部门", orderNum: 2, leader: "必哥", phone: "15888888888", email: "bg@qq.com", status: "0", delFlag: "0", parentName: null, children: []},
  {createBy: "admin", createTime: "2024-07-07 17:41:52", updateBy: null, updateTime: null, remark: null, deptId: 105, parentId: 101, ancestors: "0,100,101", deptName: "测试部门", orderNum: 3, leader: "必哥", phone: "15888888888", email: "bg@qq.com", status: "0", delFlag: "0", parentName: null, children: []},
  {createBy: "admin", createTime: "2024-07-07 17:41:52", updateBy: null, updateTime: null, remark: null, deptId: 106, parentId: 101, ancestors: "0,100,101", deptName: "财务部门", orderNum: 4, leader: "必哥", phone: "15888888888", email: "bg@qq.com", status: "0", delFlag: "0", parentName: null, children: []},
  {createBy: "admin", createTime: "2024-07-07 17:41:52", updateBy: null, updateTime: null, remark: null, deptId: 107, parentId: 101, ancestors: "0,100,101", deptName: "运维部门", orderNum: 5, leader: "必哥", phone: "15888888888", email: "bg@qq.com", status: "0", delFlag: "0", parentName: null, children: []},
  {createBy: "admin", createTime: "2024-07-07 17:41:52", updateBy: null, updateTime: null, remark: null, deptId: 108, parentId: 102, ancestors: "0,100,102", deptName: "市场部门", orderNum: 1, leader: "必哥", phone: "15888888888", email: "bg@qq.com", status: "0", delFlag: "0", parentName: null, children: []},
  {createBy: "admin", createTime: "2024-07-07 17:41:52", updateBy: null, updateTime: null, remark: null, deptId: 109, parentId: 102, ancestors: "0,100,102", deptName: "财务部门", orderNum: 2, leader: "必哥", phone: "15888888888", email: "bg@qq.com", status: "0", delFlag: "0", parentName: null, children: []}
];

const mocks: MockRecord[] = [
  {
    method: "get",
    url: "/api/system/dept/list",
    response(option) {
      const copyList = cloneDeep(deptList);

      let resList = deptList;

      if (option.query.pageNum) {
        resList = copyList.splice((option.query.pageNum - 1) * option.query.pageSize, option.query.pageSize);
      }

      const resData: responseType = {
        code: 200,
        msg: "查询成功",
        total: deptList.length,
        ...(option.query.pageNum ? { rows: resList } : { data: resList })
      }

      return resData;
    }
  },
  {
    method: "get",
    url: "/api/system/dept/:deptId",
    response(option) {
      const resDept = deptList.find(item => item.deptId == option.query.deptId);

      const resData: responseType = {
        code: resDept ? 200 : 500,
        msg: resDept ? "查询成功" : "查询失败",
        data: resDept,
      }

      return resData;
    }
  },
  {
    method: "post",
    url: "/api/system/dept",
    response(option) {
      const newDept = option.body;

      newDept.deptId = deptList.length + 1;
      deptList.unshift(newDept);

      const resData: responseType = {
        code: 200,
        msg: "新增成功",
      }

      return resData;
    }
  },
  {
    method: "put",
    url: "/api/system/dept",
    response(option) {
      const newDept = option.body;

      const index = deptList.findIndex(item => item.deptId == newDept.deptId);

      deptList.splice(index, 1, newDept);

      const resData: responseType = {
        code: 200,
        msg: "修改成功",
      }

      return resData;
    }
  },
  {
    method: "delete",
    url: "/api/system/dept/:deptIds",
    response(option) {
      const deptIds = option.query.deptIds.split(',');

      const filterList = deptList.filter(item => !deptIds.includes(String(item.deptId)));

      deptList = filterList;

      const resData: responseType = {
        code: 200,
        msg: "删除成功",
      }
      return resData;
    }
  }
];

export default mocks;